/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.A简单题.山脉数组的峰顶索引;


public class Main {

    public static void main(String arg[]) {//main--来自高金磊

//        new Main().peakIndexInMountainArray(new int[]{0, 2, 1, 0});

    }


    public int peakIndexInMountainArray(int[] arr) {
        //二分法
        int le = 1, ri = arr.length - 2;
        int middle = 1;
        while (le <= ri) {
            middle = (le - ri) / 2 + ri;
            if (arr[middle] > arr[middle + 1] && arr[middle] > arr[middle - 1]) {
                return middle;
            } else {
                if (arr[middle] < arr[middle + 1]) {
                    le = middle + 1;
                } else {
                    ri = middle - 1;
                }
            }
        }
        return middle;

    }
}
